package com.ruoyi.web.controller.cat;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.cat.domain.AdTicketClassify1;
import com.ruoyi.cat.domain.AdTicketClassify2;
import com.ruoyi.cat.service.IAdTicketClassify1Service;
import com.ruoyi.cat.service.IAdTicketClassify2Service;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分类 小类Controller
 * 
 * @author ruoyi
 * @date 2025-07-16
 */
@RestController
@RequestMapping("/system/ticketClassify2")
public class AdTicketClassify2Controller extends BaseController
{
    @Autowired
    private IAdTicketClassify2Service adTicketClassify2Service;

    @Autowired
    private IAdTicketClassify1Service adTicketClassify1Service;

    @GetMapping("/getClassify1")
    public AjaxResult getClassify1(AdTicketClassify1 adTicketClassify1)
    {
        List<AdTicketClassify1> list = adTicketClassify1Service.selectAdTicketClassify1List(adTicketClassify1);
        return success(list);
    }

    @GetMapping("/getClassifyData")
    public AjaxResult getClassifyData(AdTicketClassify1 adTicketClassify1)
    {
        List<AdTicketClassify1> list = adTicketClassify1Service.getClassifyData(adTicketClassify1);
        return success(list);
    }

    @GetMapping("/getClassify2")
    public AjaxResult getClassify2(AdTicketClassify2 adTicketClassify2)
    {
        List<AdTicketClassify2> list = adTicketClassify2Service.selectAdTicketClassify2List(adTicketClassify2);
        return success(list);
    }



    /**
     * 查询分类 小类列表
     */
    @PreAuthorize("@ss.hasPermi('system:classify2:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdTicketClassify2 adTicketClassify2)
    {
        startPage();
        List<AdTicketClassify2> list = adTicketClassify2Service.selectAdTicketClassify2List(adTicketClassify2);
        return getDataTable(list);
    }

    /**
     * 导出分类 小类列表
     */
    @PreAuthorize("@ss.hasPermi('system:classify2:export')")
    @Log(title = "分类 小类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdTicketClassify2 adTicketClassify2)
    {
        List<AdTicketClassify2> list = adTicketClassify2Service.selectAdTicketClassify2List(adTicketClassify2);
        ExcelUtil<AdTicketClassify2> util = new ExcelUtil<AdTicketClassify2>(AdTicketClassify2.class);
        util.exportExcel(response, list, "分类 小类数据");
    }

    /**
     * 获取分类 小类详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:classify2:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(adTicketClassify2Service.selectAdTicketClassify2ById(id));
    }

    /**
     * 新增分类 小类
     */
   // @PreAuthorize("@ss.hasPermi('system:classify2:add')")
   // @Log(title = "分类 小类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdTicketClassify2 adTicketClassify2)
    {
        return toAjax(adTicketClassify2Service.insertAdTicketClassify2(adTicketClassify2));
    }

    /**
     * 修改分类 小类
     */
    @PreAuthorize("@ss.hasPermi('system:classify2:edit')")
    @Log(title = "分类 小类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdTicketClassify2 adTicketClassify2)
    {
        return toAjax(adTicketClassify2Service.updateAdTicketClassify2(adTicketClassify2));
    }

    /**
     * 删除分类 小类
     */
    @PreAuthorize("@ss.hasPermi('system:classify2:remove')")
    @Log(title = "分类 小类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(adTicketClassify2Service.deleteAdTicketClassify2ByIds(ids));
    }

    @GetMapping("/moveUp")
    public AjaxResult moveUp(@RequestParam Long id) {
        try {
            // 获取当前记录
            AdTicketClassify2 current = adTicketClassify2Service.selectAdTicketClassify2ById(id);
            if (current == null) {
                return AjaxResult.error("记录不存在");
            }

            // 查找比当前sort值大的最小记录（即上一条记录）
            AdTicketClassify2 previous = adTicketClassify2Service.selectPreviousRecord1(current.getSort());
            if (previous == null) {
                return AjaxResult.error("已经是第一条记录，无法上移");
            }

            // 交换sort值
            Long tempSort = current.getSort();
            current.setSort(previous.getSort());
            previous.setSort(tempSort);

            // 更新数据库
            adTicketClassify2Service.updateAdTicketClassify2(current);
            adTicketClassify2Service.updateAdTicketClassify2(previous);

            return AjaxResult.success("上移成功");
        } catch (Exception e) {
            return AjaxResult.error("上移失败：" + e.getMessage());
        }
    }

    /**
     * 下移
     * @param id 要下移的记录ID
     * @return AjaxResult
     */
    @GetMapping("/moveDown")
    public AjaxResult moveDown(@RequestParam Long id) {
        try {
            // 获取当前记录
            AdTicketClassify2 current = adTicketClassify2Service.selectAdTicketClassify2ById(id);
            if (current == null) {
                return AjaxResult.error("记录不存在");
            }

            // 查找比当前sort值小的最大记录（即下一条记录）
            AdTicketClassify2 next = adTicketClassify2Service.selectNextRecord1(current.getSort());
            if (next == null) {
                return AjaxResult.error("已经是最后一条记录，无法下移");
            }

            // 交换sort值
            Long tempSort = current.getSort();
            current.setSort(next.getSort());
            next.setSort(tempSort);

            // 更新数据库
            adTicketClassify2Service.updateAdTicketClassify2(current);
            adTicketClassify2Service.updateAdTicketClassify2(next);

            return AjaxResult.success("下移成功");
        } catch (Exception e) {
            return AjaxResult.error("下移失败：" + e.getMessage());
        }
    }
}
